//
//import 'dart:convert';
//import 'dart:core';
//
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/basewidget/HttpUtils/HttpBaseConst.dart';
import 'package:flutter_ahj_app/base/bean/show_files_bean.dart';

import '../HttpUtils.dart';


class PhotoWidget extends StatefulWidget {
  final String fileName; //请求接口获得图片的名字
  final String defaultFilesName; //默认显示的文件图标名称
  final double width; //宽
  final double height; //高

  PhotoWidget(
      {this.fileName,
      this.width,
      this.height,
      this.defaultFilesName = "默认",
      Key key})
      : super(key: key);

  @override
  State createState() => PhotoWidgetState();
}

class PhotoWidgetState extends State<PhotoWidget> {
  String _filePath;
  bool _isLocalFile = false;

  ///网络下载图片的地址
  @override
  void initState() {
    super.initState();
    if (widget.fileName == null || widget.fileName == "") {
      return _filePath = null;
    } else {
      if (widget.fileName.startsWith("https://") ||
          widget.fileName.startsWith("http://")) {
        _filePath = widget.fileName;
      } else {
        List fileNameList = widget.fileName.split("/");
        if (fileNameList.length == 2) {
          _isLocalFile = false;
          Future.delayed(Duration.zero, () {
            _getImagePath();
          });
        } else {
          _isLocalFile = true;
          _filePath = widget.fileName;
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return _filePath == null
        ? ClipOval(
            child: Image.asset('img/iv_head_none.png',
                width: widget.width, height: widget.height),
          )
        : (_isLocalFile
            ? ClipOval(
                child: Image.asset(_filePath,
                    width: widget.width, height: widget.height),
              )
            : ClipOval(
                child: FadeInImage.assetNetwork(
                  placeholder: 'img/iv_head_none.png',
                  image: _filePath,
                  width: widget.width,
                  height: widget.height,
                  fit: BoxFit.cover,
                ),
              ));
  }

  ///获得图片路径
  _getImagePath() async {
    var params = {"a": "a"};

    /// type  是否是视频1:是 0:不是
    String url = HttpBaseConst.serverUrl +
        HttpBaseConst.downloadPath +
        "?" +
        "fileName=" +
        widget.fileName +
        "&type=0";
    HttpUtils.httpGet(url, params, (result) {
      print(result);
      ShowFilesBean showFilesBean = ShowFilesBean.fromJson(json.decode(result));
      setState(() {
        _filePath = showFilesBean.data.fileUrl;
      });
    }, context: context);
  }
}
